package cn.kgc.reflect;

import cn.kgc.annotation.Bean;

import java.lang.annotation.Annotation;

/**
 * @author: mosin
 * @version: v1.0  2022/10/21
 * 工厂对象
 */
public class BeanFactory {

    public  static  Object  getBean(String name){
        try {
            Class clazz = Class.forName(name);
            // clazz上必须包含Bean注解
            Annotation annotation = clazz.getAnnotation(Bean.class);
            if(annotation!=null){
                Object o = clazz.newInstance();
                return  o;
            }else {
                System.out.println("没有bean注解不能创建对象");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    public static void main(String[] args) {

        Object bean = BeanFactory.getBean("cn.kgc.reflect.Person");
        System.out.println("bean = " + bean);

    }
}
